﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApplLogic.ApplClasses;
using ApplLogic.DbToApplConvertor;
using Entities;
using DAL;

namespace ApplLogic.ApplDbMediators
{
    public class ApplDbShiftMediator 
    {
        private DataService m_DataService = DataService.Instance;

        private static volatile ApplDbShiftMediator s_Instance = null;
        private static object s_InstanceLockObj = new Object();

        private ApplDbShiftMediator() { }

        public static ApplDbShiftMediator Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    lock (s_InstanceLockObj)
                    {
                        if (s_Instance == null)
                        {
                            s_Instance = new ApplDbShiftMediator();
                        }
                    }
                }

                return s_Instance;
            }
        }

        public void AddShift(ApplShift shiftToAdd, bool saveChanges = false)
        {
            Shift dbShift = DbApplObjectConvertor.CreateShiftFromApplShift(shiftToAdd);
            m_DataService.AddShift(dbShift, saveChanges);
        }

        public ApplShift GetShiftByDateRange(DateTime startTime, DateTime endTime)
        {
            Shift shift = m_DataService.GetShiftByDateRange(startTime, endTime);
            return DbApplObjectConvertor.CreateApplShiftFromDbShift(shift);
        }

        public void DeleteShift(string shiftId, bool saveChanges = false)
        {
            m_DataService.DeleteShift(shiftId, saveChanges);
        }

        public ApplShift GetShift(string shiftId)
        {
            Shift dbShift = m_DataService.GetShiftByID(shiftId);
            return DbApplObjectConvertor.CreateApplShiftFromDbShift(dbShift);
        }

        public List<ApplShift> GetAllShifts()
        {
            List<ApplShift> allShifts = new List<ApplShift>();
            foreach (Shift shift in m_DataService.GetAllShifts())
            {
                allShifts.Add(DbApplObjectConvertor.CreateApplShiftFromDbShift(shift));
            }
            return allShifts;
        }

        public List<ApplShift> GetShiftsByRange(DateTime start, DateTime end)
        {
            List<ApplShift> allShiftsByRange = new List<ApplShift>();
            foreach (Shift shift in m_DataService.GetShiftsByRange(start, end))
            {
                allShiftsByRange.Add(DbApplObjectConvertor.CreateApplShiftFromDbShift(shift));
            }
            return allShiftsByRange;    
        }

        public List<string> GetAllShiftsIds()
        {
            List<string> allShiftsids = new List<string>();
            foreach (Shift shift in m_DataService.GetAllShifts())
            {
                ApplShift applShift = DbApplObjectConvertor.CreateApplShiftFromDbShift(shift);
                allShiftsids.Add(applShift.Id);
            }
            return allShiftsids;
        }

        public void UpdateShiftName(string shiftId, string newName, bool saveChanges = false)
        {
            m_DataService.UpdateShiftName(shiftId, newName, saveChanges);

        }

        public void UpdateNumberOfWorkersNeeded(string shiftId, int newNumberNeeded, bool saveChanges = false)
        {
            m_DataService.UpdateNumberOfWorkersNeeded(shiftId, newNumberNeeded, saveChanges);
        }

        public void UpdateShift(ApplShift updatedApplShift)
        {
            UpdateShiftName(updatedApplShift.Id, updatedApplShift.Name);
            UpdateNumberOfWorkersNeeded(updatedApplShift.Id, updatedApplShift.NumberOfWorkersNeeded);
            m_DataService.SaveChanges();
        }

        public string GenerateIdForShift()
        {
            string id = string.Empty;
            List<string> shiftsIds = GetAllShiftsIds();
            if (shiftsIds.Count == 0)
            {
                return "1";
            }
            int maxIdExist = shiftsIds.Select(int.Parse).Concat(new[] { -1 }).Max();
            return (maxIdExist + 1).ToString();
        }

    }
}
